package com.demo.validate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 电话校验
 *
 * @author sdyang
 * @create 2019-01-22 17:29
 **/
public class PhoneValidate extends FieldDataValidate {

    @Override
    public boolean check(Object object) {
        String str = object.toString();
        Pattern p1 = null, p2 = null;
        Matcher m = null;
        boolean b = false;
        p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的
        if (str.length() > 9) {
            m = p1.matcher(str);
            b = m.matches();
        } else {
            m = p2.matcher(str);
            b = m.matches();
        }
        return b;

    }
}
